<template>
  <div class="edit-box">
    <el-dialog width="500px" :title="dialogTitle" v-model="dialogFormVisible" :draggable="true" @close="dialogClose">
      <el-form ref="dialogFormRef" :model="dialogForm" :rules="dialogFormRules">
        <el-row>
          <el-col :span="12">
            <el-form-item prop="shopId" label="商户">
              <el-select :disabled="dialogForm.appid" v-model="dialogForm.shopId" filterable placeholder="请选择商户" clearable>
                <el-option v-for="item in shopOptions" :key="item.shopId" :label="item.shopName" :value="item.shopId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="flag" label="状态">
              <el-radio-group v-model="dialogForm.flag">
                <el-radio :label="1">启用</el-radio>
                <el-radio :label="2">禁用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item prop="allowIps" label="允许访问的ip">
          <el-input v-model="dialogForm.allowIps" clearable placeholder="请输入允许访问的ip" />
        </el-form-item>
        <el-form-item prop="notifyUrl" label="回调地址">
          <el-input v-model="dialogForm.notifyUrl" clearable placeholder="请输入回调地址" />
        </el-form-item>
        <!-- <el-divider border-style="dashed" />
        <el-row>
          <el-col :span="12">
            <el-form-item prop="password" label="登录密码">
              <el-input style="width:90%" v-model="dialogForm.password" type="password" show-password placeholder="请输入登录密码" />
            </el-form-item>
          </el-col>
        </el-row> -->
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="submitHandle">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { reactive, ref, toRefs } from 'vue'
import api from '../api'
import hexMD5 from "@/utils/md5.min.js"
import { successDeal, warningDeal } from '@/utils/utils'
export default {
  setup(props, context) {
    const dialogFormRef = ref(null)

    const state = reactive({
      dialogTitle: '',
      dialogFormVisible: false,
      dialogForm: {
        flag: 1,
      },
      dialogFormRules: {
        shopId: [
          { required: true, message: '请选择商户', trigger: 'blur' },
        ],
        allowIps: [
          { required: true, message: '请输入允许访问的ip', trigger: 'blur' },
        ],
        notifyUrl: [
          { required: false, message: '请输入回调地址', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
        ],
      },
      shopOptions: [],
    })

    function dialogShow(type, data) {
      if (type == 'add') {
        state.dialogTitle = '新增下游接口'
      } else {
        state.dialogTitle = '编辑下游接口'
      }
      if (data) {
        getDetail(data.appid)
      }
      getShop()
      state.dialogFormVisible = true
    }

    function getShop() {
      api.shopListAll().then(res => {
        if (res.code == '200') {
          state.shopOptions = res.data
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function getDetail(appid) {
      const obj = {
        appid
      }
      api.apiUserDetail(obj).then(res => {
        if (res.code == '200') {
          state.dialogForm = res.data
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function submitHandle() {
      dialogFormRef.value.validate((valid, fields) => {
        if (valid) {
          if (state.dialogForm.appid) {
            const obj = {
              appid: state.dialogForm.appid || null,
              flag: state.dialogForm.flag || null,
              allowIps: state.dialogForm.allowIps || null,
              notifyUrl: state.dialogForm.notifyUrl || null,
              // password: hexMD5(state.dialogForm.password),
            }
            api.apiUserModify(obj).then(res => {
              if (res.code == '200') {
                successDeal(res.msg)
                context.emit('dialogClose')
                state.dialogFormVisible = false
              } else {
                warningDeal(res.msg)
              }
            }).catch(err => {
              console.log(err)
            })
          } else {
            const obj = {
              shopId: state.dialogForm.shopId || null,
              flag: state.dialogForm.flag || null,
              allowIps: state.dialogForm.allowIps || null,
              notifyUrl: state.dialogForm.notifyUrl || null,
              // password: hexMD5(state.dialogForm.password),
            }
            api.apiUserAdd(obj).then(res => {
              if (res.code == '200') {
                successDeal(res.msg)
                context.emit('dialogClose')
                state.dialogFormVisible = false
              } else {
                warningDeal(res.msg)
              }
            }).catch(err => {
              console.log(err)
            })
          }
        }
      })
    }

    function dialogClose() {
      state.dialogForm = {
        flag: 1,
      }
      dialogFormRef.value.resetFields()
    }

    return {
      ...toRefs(state),
      dialogFormRef,
      dialogShow,
      dialogClose,
      submitHandle,
    }
  }
}
</script>

<style>
</style>